package com.msmall.community.common.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.exception.DataNotFoundException;
import com.msmall.base.pager.MatchType;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CommunityInitialize;
import com.msmall.community.common.dao.ScoreRuleDao;
import com.msmall.community.common.model.ScoreRule;
import com.msmall.community.common.service.ScoreRuleManager;
import com.msmall.community.user.service.UserManager;

public class ScoreRuleManagerImpl extends BaseManagerImpl<ScoreRule, Long> implements ScoreRuleManager{

	private static final Log logger = LogFactory.getLog(ScoreRuleManagerImpl.class);
	
	private ScoreRuleDao scoreRuleDao;
	
	private UserManager userManager;
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public void setScoreRuleDao(ScoreRuleDao scoreRuleDao) {
		this.scoreRuleDao = scoreRuleDao;
	}

	public ScoreRule getScoreRule(String key) throws Exception{
		ScoreRule scoreRule = (ScoreRule) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_SCORERULE).get(key);
		if(scoreRule == null){
			scoreRule = loadScoreRule(key);
		}
		return scoreRule;
	}
	
	public ScoreRule loadScoreRule(String key) throws Exception{
		try{
			List list = scoreRuleDao.getByProperty(ScoreRule.class, "scoreKey", MatchType.EQ, String.valueOf(key));
			if(null != list && list.size() > 0)
				return (ScoreRule) list.get(0);
			throw new DataNotFoundException("ScoreRule not found with key :" + key);
		} catch(Exception e){
			throw e;
		}
	}
	
	public void saveScoreRule(ScoreRule gradeRule) {
		scoreRuleDao.save(gradeRule);
		CommunityInitialize.refreshScoreRule();
	}
}
